package com.mgface.mrmdb.listener;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;


/**
 * 
 * ApplicationListener#ContextRefreshedEvent上下文更新完之后触发事件<br/>
 * 实现servletContextAware接口，会在初始化当前Bean的时候，自动注入servletContext这个实例[Bean]<br/>
 * 这个servletContext Bean它是在ApplicationContext#refresh更新上下文的时候注入进来了<br/> 
 *
 */
@Component("initializedListener")
public class InitializedListener implements ApplicationListener<ContextRefreshedEvent>, ServletContextAware {

	private ServletContext servletContext;

	private Log log = LogFactory.getLog(this.getClass());
	
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
	
	public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
		if ((contextRefreshedEvent.getApplicationContext().getParent() == null)) {
			log.error("初始化:"+contextRefreshedEvent.getApplicationContext().getParent());
		}else{
			log.error("error:"+contextRefreshedEvent.getApplicationContext().getParent());
		}
	}
}

